

<!DOCTYPE html>
<html lang="zh-CN">

<head><meta name="generator" content="Hexo 3.9.0">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Jetpack 主要依赖关系 - 杨云召 | 博客</title>
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta name="google" content="notranslate">

  
  
  <meta name="description" content="介绍 Jetpack 主要组件的依赖关系，初学必看。
..."> 
  
  <meta name="author" content="杨云召（Flywith24）"> 

  
    <link rel="icon" href="/images/icons/favicon-16x16.png" type="image/png" sizes="16x16">
  
  
    <link rel="icon" href="/images/icons/favicon-32x32.png" type="image/png" sizes="32x32">
  
  
    <link rel="apple-touch-icon" href="/images/icons/apple-touch-icon.png" sizes="180x180">
  
  
    <meta rel="mask-icon" href="/images/icons/stun-logo.svg" color="#333333">
  
  
    <meta rel="msapplication-TileImage" content="/images/icons/favicon-144x144.png">
    <meta rel="msapplication-TileColor" content="#000000">
  

  <link rel="stylesheet" href="/css/style.css">

  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1445822_hhodbqn7tit.css">
  

  
  
  <link rel="stylesheet" href="https://cdn.bootcss.com/fancybox/3.5.7/jquery.fancybox.min.css">
  

  
  
  <link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.18.1/styles/xcode.min.css">
  

  <script>
    var CONFIG = window.CONFIG || {};
    var ZHAOO = window.ZHAOO || {};
    CONFIG = {
      isHome: false,
      fancybox: true,
      pjax: false,
      lazyload: {
        enable: true,
        loadingImage: '/images/theme/loading.gif',
      },
      donate: {
        enable: true,
        alipay: 'https://gitee.com/flywith24/Album/raw/master/img/20201015113301.jpg',
        wechat: 'https://gitee.com/flywith24/Album/raw/master/img/20201015112814.png'
      },
      motto: {
        api: '',
        default: '不人云亦云，只求接近真相.'
      },
      galleries: {
        enable: true
      },
      fab: {
        enable: true,
        alwaysShow: false
      },
      carrier: {
        enable: true
      },
      daovoice: {
        enable: true
      }
    }
  </script>

  

  
</head></html>
<body class="lock-screen">
  <div class="loading"></div>
  


<nav class="navbar">
  <div class="left"></div>
  <div class="center">Jetpack 主要依赖关系</div>
  <div class="right">
    <i class="iconfont iconmenu j-navbar-menu"></i>
  </div>
</nav>

  <nav class="menu">
  <div class="menu-wrap">
    <div class="menu-close">
      <i class="iconfont iconbaseline-close-px"></i>
    </div>
    <ul class="menu-content">
      
      
      
      
      <li class="menu-item"><a href="/ " class="underline"> 首页</a></li>
      
      
      
      
      <li class="menu-item"><a href="/archives " class="underline"> 归档</a></li>
      
      
      
      
      <li class="menu-item"><a href="/tags " class="underline"> 标签</a></li>
      
      
      
      
      <li class="menu-item"><a href="/categories " class="underline"> 分类</a></li>
      
      
      
      
      <li class="menu-item"><a href="/about " class="underline"> 关于</a></li>
      
    </ul>
    <div class="menu-copyright"><p>Copyright© 2017 - 2020 ❤️ Flywith24</p></div>
  </div>
</nav>
  <main id="main">
  <div class="container" id="container">
    <article class="article">
  <div class="wrap">
    <section class="head">
  <img   class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20201015093950.png" src=""  draggable="false">
  <div class="head-mask">
    <h1 class="head-title">Jetpack 主要依赖关系</h1>
    <div class="head-info">
      <span class="post-info-item"><i class="iconfont iconcalendar"></i>三月 01, 2020</span
        class="post-info-item">
      
      <span class="post-info-item"><i class="iconfont iconfont-size"></i>9147</span>
    </div>
  </div>
</section>
    <section class="main">
      <section class="content">
        <blockquote>
<p>在学习和使用 <code>jetpack</code> 组件时，总是被其 gradle 依赖搞的晕头转向，故在此整理 <code>jetpack</code> 主要组件的依赖，及传递关系</p>
</blockquote>
<ul>
<li><a href="https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-master-dev" target="_blank" rel="noopener"><code>jetpcak</code> 组件源码地址</a></li>
<li>查询 <code>jetpcak</code> 组件 版本: <a href="https://maven.google.com/web/index.html" target="_blank" rel="noopener">Google’s Maven Repository</a></li>
<li>查看依赖树：在项目根目录下执行<code>./gradlew :app:dependencies</code></li>
</ul>
<p><strong>可直接跳过后续内容前往最后一节查看总结</strong></p>
<p>关于 <code>androidx</code>下 <code>fragment/activity</code>的变化，可查看该文, <a href="https://juejin.im/post/5e5a0c316fb9a07cd248d29e" target="_blank" rel="noopener">【译】AdroidX下使用Activity和Fragment的变化</a></p>
<h3 id="Appcompat"><a href="#Appcompat" class="headerlink" title="Appcompat"></a>Appcompat</h3><h4 id="引入"><a href="#引入" class="headerlink" title="引入"></a>引入</h4><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs groovy">dependencies &#123;<br>    <span class="hljs-keyword">def</span> appcompat_version = <span class="hljs-string">"1.1.0"</span><br><br>    implementation <span class="hljs-string">"androidx.appcompat:appcompat:$appcompat_version"</span><br>    <span class="hljs-comment">// For loading and tinting drawables on older versions of the platform</span><br>    implementation <span class="hljs-string">"androidx.appcompat:appcompat-resources:$appcompat_version"</span><br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="依赖树"><a href="#依赖树" class="headerlink" title="依赖树"></a>依赖树</h4><p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200507093256.png" src="/2020/03/01/Jetpack-dependencies/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt></p>
<h4 id="传递依赖"><a href="#传递依赖" class="headerlink" title="传递依赖"></a>传递依赖</h4><p><code>androidx.annotation:annotation:1.1.0</code></p>
<p><code>androidx.core:core:1.1.0</code></p>
<p><code>androidx.cursoradapter:cursoradapter:1.0.0</code></p>
<p><code>androidx.fragment:fragment:1.1.0</code></p>
<p><code>androidx.appcompat:appcompat-resources:1.1.0</code></p>
<p><code>androidx.drawerlayout:drawerlayout:1.0.0</code></p>
<p><code>androidx.collection:collection:1.0.0</code></p>
<blockquote>
<p><strong><code>appcompat</code> 中默认引入了 <code>fragment</code> 库，如果想使用更新版本的 <code>fragment</code> 库，可以单独引用</strong></p>
</blockquote>
<blockquote>
<p><a href="https://android.googlesource.com/platform/frameworks/support/+/b0b973c9bc89f9ad3efe3c06e41b72957f032a99/appcompat/build.gradle" target="_blank" rel="noopener">appcompat build.gradle 源码地址</a></p>
</blockquote>
<h3 id="Fragment"><a href="#Fragment" class="headerlink" title="Fragment"></a>Fragment</h3><h4 id="引入-1"><a href="#引入-1" class="headerlink" title="引入"></a>引入</h4><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs groovy">dependencies &#123;<br>    <span class="hljs-keyword">def</span> fragment_version = <span class="hljs-string">"1.2.2"</span><br><br>    <span class="hljs-comment">// Java language implementation</span><br>    implementation <span class="hljs-string">"androidx.fragment:fragment:$fragment_version"</span><br>    <span class="hljs-comment">// Kotlin</span><br>    implementation <span class="hljs-string">"androidx.fragment:fragment-ktx:$fragment_version"</span><br>    <span class="hljs-comment">// Testing Fragments in Isolation</span><br>    implementation <span class="hljs-string">"androidx.fragment:fragment-testing:$fragment_version"</span><br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>⚠️ Note: The Kotlin dependant libraries of this version (fragment-ktx,fragment-testing) target Java 8 programming language bytecode. <a href="https://developer.android.com/studio/write/java8-support" target="_blank" rel="noopener">Please read Use Java 8 language features to learn how to use it in your project.</a></p>
</blockquote>
<h4 id="依赖树-1"><a href="#依赖树-1" class="headerlink" title="依赖树"></a>依赖树</h4><p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200507093455.png" src="/2020/03/01/Jetpack-dependencies/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt></p>
<h4 id="传递依赖-1"><a href="#传递依赖-1" class="headerlink" title="传递依赖"></a>传递依赖</h4><p><code>org.jetbrains.kotlin:kotlin-stdlib:1.3.50</code></p>
<p><code>androidx.activity:activity-ktx:1.1.0</code></p>
<p><code>androidx.core:core-ktx:1.1.0</code></p>
<p><code>androidx.collection:collection-ktx:1.1.0</code></p>
<p><code>androidx.lifecycle:lifecycle-livedata-core-ktx:2.2.0</code></p>
<p><code>androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0</code></p>
<blockquote>
<p><strong><code>fragment</code> 库默认引入了 <code>activity</code> <code>core-ktx</code> <code>lifecycle-livedata-core-ktx</code> <code>lifecycle-viewmodel-ktx</code> 库</strong>  </p>
<p><a href="https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-master-dev/fragment/fragment-ktx/build.gradle" target="_blank" rel="noopener">fragment build.grdle 源码地址</a></p>
</blockquote>
<h3 id="Activity"><a href="#Activity" class="headerlink" title="Activity"></a>Activity</h3><h4 id="引入-2"><a href="#引入-2" class="headerlink" title="引入"></a>引入</h4><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs groovy">dependencies &#123;<br>    <span class="hljs-keyword">def</span> activity_version = <span class="hljs-string">"1.1.0"</span><br><br>    <span class="hljs-comment">// Java language implementation</span><br>    implementation <span class="hljs-string">"androidx.activity:activity:$activity_version"</span><br>    <span class="hljs-comment">// Kotlin</span><br>    implementation <span class="hljs-string">"androidx.activity:activity-ktx:$activity_version"</span><br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>⚠️ Note: The Kotlin dependant libraries of this version (activity-ktx) target Java 8 programming language bytecode. <a href="https://developer.android.com/studio/write/java8-support" target="_blank" rel="noopener">Please read Use Java 8 language features to learn how to use it in your project.</a></p>
</blockquote>
<h4 id="依赖树-2"><a href="#依赖树-2" class="headerlink" title="依赖树"></a>依赖树</h4><p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200507093743.png" src="/2020/03/01/Jetpack-dependencies/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt></p>
<h4 id="依赖传递"><a href="#依赖传递" class="headerlink" title="依赖传递"></a>依赖传递</h4><p><code>org.jetbrains.kotlin:kotlin-stdlib:1.3.50</code></p>
<p><code>androidx.core:core-ktx:1.1.0</code></p>
<p><code>androidx.lifecycle:lifecycle-runtime-ktx:2.2.0</code></p>
<p><code>androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0</code></p>
<blockquote>
<p><a href="https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-master-dev/activity/activity-ktx/build.gradle" target="_blank" rel="noopener">activity build.gradle 源码地址</a></p>
</blockquote>
<h3 id="Core"><a href="#Core" class="headerlink" title="Core"></a>Core</h3><h4 id="引入-3"><a href="#引入-3" class="headerlink" title="引入"></a>引入</h4><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs groovy">dependencies &#123;<br>    <span class="hljs-keyword">def</span> core_version = <span class="hljs-string">"1.2.0"</span><br><br>    <span class="hljs-comment">// Java language implementation</span><br>    implementation <span class="hljs-string">"androidx.core:core:$core_version"</span><br>    <span class="hljs-comment">// Kotlin</span><br>    implementation <span class="hljs-string">"androidx.core:core-ktx:$core_version"</span><br><br>    <span class="hljs-comment">// To use RoleManagerCompat</span><br>    implementation <span class="hljs-string">"androidx.core:core-role:1.0.0-alpha01"</span><br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="依赖树-3"><a href="#依赖树-3" class="headerlink" title="依赖树"></a>依赖树</h4><p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200507093859.png" src="/2020/03/01/Jetpack-dependencies/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt></p>
<h3 id="Lifecycle"><a href="#Lifecycle" class="headerlink" title="Lifecycle"></a>Lifecycle</h3><h4 id="引入-4"><a href="#引入-4" class="headerlink" title="引入"></a>引入</h4><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs groovy">dependencies &#123;<br>    <span class="hljs-keyword">def</span> lifecycle_version = <span class="hljs-string">"2.2.0"</span><br>    <span class="hljs-keyword">def</span> arch_version = <span class="hljs-string">"2.1.0"</span><br><br>    <span class="hljs-comment">// ViewModel</span><br>    implementation <span class="hljs-string">"androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"</span><br>    <span class="hljs-comment">// LiveData</span><br>    implementation <span class="hljs-string">"androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"</span><br>    <span class="hljs-comment">// Lifecycles only (without ViewModel or LiveData)</span><br>    implementation <span class="hljs-string">"androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"</span><br><br>    <span class="hljs-comment">// Saved state module for ViewModel</span><br>    implementation <span class="hljs-string">"androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"</span><br><br>    <span class="hljs-comment">// Annotation processor</span><br>    kapt <span class="hljs-string">"androidx.lifecycle:lifecycle-compiler:$lifecycle_version"</span><br>    <span class="hljs-comment">// alternately - if using Java8, use the following instead of lifecycle-compiler</span><br>    implementation <span class="hljs-string">"androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"</span><br><br>    <span class="hljs-comment">// optional - helpers for implementing LifecycleOwner in a Service</span><br>    implementation <span class="hljs-string">"androidx.lifecycle:lifecycle-service:$lifecycle_version"</span><br><br>    <span class="hljs-comment">// optional - ProcessLifecycleOwner provides a lifecycle for the whole application process</span><br>    implementation <span class="hljs-string">"androidx.lifecycle:lifecycle-process:$lifecycle_version"</span><br><br>    <span class="hljs-comment">// optional - ReactiveStreams support for LiveData</span><br>    implementation <span class="hljs-string">"androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version"</span><br><br>    <span class="hljs-comment">// optional - Test helpers for LiveData</span><br>    testImplementation <span class="hljs-string">"androidx.arch.core:core-testing:$arch_version"</span><br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<ul>
<li>⚠️ <strong><code>lifecycle-extensions</code> 已废弃，如果使用 <code>LifecycleService</code> 请依赖 <code>lifecycle-service</code>；如果使用 <code>ProcessLifecycleOwner</code> 请依赖 <code>lifecycle-process</code>。<code>lifecycle-extensionsl</code>不会有2.3.0版本</strong></li>
<li><strong>2.1.0 后 <code>ViewModelProviders.of()</code> 被废弃。您可以在 <code>FragmentActivity</code> 或者 <code>Fragment</code> 使用 <code>ViewModelProvider(ViewModelStoreOwner)</code> 构造器来实现相同的功能。（<code>Fragment</code> 库 1.2.0以上）</strong></li>
</ul>
</blockquote>
<h4 id="依赖树-4"><a href="#依赖树-4" class="headerlink" title="依赖树"></a>依赖树</h4><p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200507094042.png" src="/2020/03/01/Jetpack-dependencies/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="livedata"></p>
<p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200507094223.png" src="/2020/03/01/Jetpack-dependencies/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="viewmodel"></p>
<h3 id="Navigation"><a href="#Navigation" class="headerlink" title="Navigation"></a>Navigation</h3><h4 id="引入-5"><a href="#引入-5" class="headerlink" title="引入"></a>引入</h4><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs groovy">dependencies &#123;<br>  <span class="hljs-keyword">def</span> nav_version = <span class="hljs-string">"2.3.0-alpha02"</span><br><br>  <span class="hljs-comment">// Java language implementation</span><br>  implementation <span class="hljs-string">"androidx.navigation:navigation-fragment:$nav_version"</span><br>  implementation <span class="hljs-string">"androidx.navigation:navigation-ui:$nav_version"</span><br><br>  <span class="hljs-comment">// Kotlin</span><br>  implementation <span class="hljs-string">"androidx.navigation:navigation-fragment-ktx:$nav_version"</span><br>  implementation <span class="hljs-string">"androidx.navigation:navigation-ui-ktx:$nav_version"</span><br><br>  <span class="hljs-comment">// Dynamic Feature Module Support</span><br>  implementation <span class="hljs-string">"androidx.navigation:navigation-dynamic-features-fragment:$nav_version"</span><br><br>  <span class="hljs-comment">// Testing Navigation</span><br>  androidTestImplementation <span class="hljs-string">"androidx.navigation:navigation-testing:$nav_version"</span><br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="依赖树-5"><a href="#依赖树-5" class="headerlink" title="依赖树"></a>依赖树</h4><p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200507094643.png" src="/2020/03/01/Jetpack-dependencies/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="navigation"></p>
<p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200507094711.png" src="/2020/03/01/Jetpack-dependencies/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="navigation-ui"></p>
<h3 id="Paging"><a href="#Paging" class="headerlink" title="Paging"></a>Paging</h3><h4 id="引入-6"><a href="#引入-6" class="headerlink" title="引入"></a>引入</h4><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs groovy">dependencies &#123;<br>  <span class="hljs-keyword">def</span> paging_version = <span class="hljs-string">"2.1.1"</span><br><br>  implementation <span class="hljs-string">"androidx.paging:paging-runtime:$paging_version"</span> <span class="hljs-comment">// For Kotlin use paging-runtime-ktx</span><br><br>  <span class="hljs-comment">// alternatively - without Android dependencies for testing</span><br>  testImplementation <span class="hljs-string">"androidx.paging:paging-common:$paging_version"</span> <span class="hljs-comment">// For Kotlin use paging-common-ktx</span><br><br>  <span class="hljs-comment">// optional - RxJava support</span><br>  implementation <span class="hljs-string">"androidx.paging:paging-rxjava2:$paging_version"</span> <span class="hljs-comment">// For Kotlin use paging-rxjava2-ktx</span><br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="依赖树-6"><a href="#依赖树-6" class="headerlink" title="依赖树"></a>依赖树</h4><p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200507094857.png" src="/2020/03/01/Jetpack-dependencies/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="paging"></p>
<h3 id="Room"><a href="#Room" class="headerlink" title="Room"></a>Room</h3><h4 id="引入-7"><a href="#引入-7" class="headerlink" title="引入"></a>引入</h4><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs groovy">dependencies &#123;<br>  <span class="hljs-keyword">def</span> room_version = <span class="hljs-string">"2.2.4"</span><br><br>  implementation <span class="hljs-string">"androidx.room:room-runtime:$room_version"</span><br>  annotationProcessor <span class="hljs-string">"androidx.room:room-compiler:$room_version"</span> <span class="hljs-comment">// For Kotlin use kapt instead of annotationProcessor</span><br><br>  <span class="hljs-comment">// optional - Kotlin Extensions and Coroutines support for Room</span><br>  implementation <span class="hljs-string">"androidx.room:room-ktx:$room_version"</span><br><br>  <span class="hljs-comment">// optional - RxJava support for Room</span><br>  implementation <span class="hljs-string">"androidx.room:room-rxjava2:$room_version"</span><br><br>  <span class="hljs-comment">// optional - Guava support for Room, including Optional and ListenableFuture</span><br>  implementation <span class="hljs-string">"androidx.room:room-guava:$room_version"</span><br><br>  <span class="hljs-comment">// Test helpers</span><br>  testImplementation <span class="hljs-string">"androidx.room:room-testing:$room_version"</span><br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>⚠️ Note: For Kotlin-based apps, make sure you use kapt instead of annotationProcessor. You should also add the kotlin-kapt plugin.</p>
</blockquote>
<h4 id="依赖树-7"><a href="#依赖树-7" class="headerlink" title="依赖树"></a>依赖树</h4><p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200507094945.png" src="/2020/03/01/Jetpack-dependencies/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="room"></p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><h4 id="版本说明"><a href="#版本说明" class="headerlink" title="版本说明"></a>版本说明</h4><blockquote>
<p><code>androidx</code>库遵循严格的语义版本控制。版本字符串（例如 1.0.1-beta02）包含三个数字，分别代表 major 级别、minor 级别和问题修复级别。预发布版本也有一个后缀，用于指定预发布阶段（Alpha 版、Beta 版、候选版本）和版本号（01、02 等）。</p>
</blockquote>
<p>库的每个版本都要经历三个预发布阶段，才能成为稳定版本。各预发布阶段的标准如下：</p>
<p><strong>Alpha 版</strong></p>
<ul>
<li>Alpha 版功能稳定，但功能可能不完整。</li>
<li>在版本处于 Alpha 版状态时，可以添加、移除或更改 API。</li>
</ul>
<p><strong>Beta 版</strong></p>
<ul>
<li>Beta 版功能稳定，并且具有功能完整的 API Surface。<br>它们可以投入实际使用，但可能包含错误。</li>
<li>Beta 版无法使用实验性编译器功能（例如 @UseExperimental）。</li>
<li>其他库的依赖项必须为 Beta 版、RC 版或稳定版。不允许使用 Alpha 版依赖项。</li>
</ul>
<p><strong>候选版本 (RC)</strong></p>
<ul>
<li>候选版本是未来的稳定版。</li>
<li>此版本可能包含在最后一刻提供的重要修复。</li>
<li>此版本的 API Surface 无法更改。</li>
<li>其他库的依赖项只能是 RC 版或稳定版。</li>
</ul>
<p>一个库可以同时具有多个版本。每个版本都具有不同的发布阶段。例如，虽然 androidx.activity 的稳定版可以是 1.0.0，但也可能还有 1.1.0-beta02 版本以及 2.0.0-alpha01 版本。</p>
<h4 id="kotlin-协程的使用"><a href="#kotlin-协程的使用" class="headerlink" title="kotlin 协程的使用"></a>kotlin 协程的使用</h4><blockquote>
<p><code>ViewModel</code> <code>LiveData</code> <code>Activity</code> <code>Fragment</code> <code>Service</code>等均可使用协程</p>
</blockquote>
<ul>
<li>对于 <code>ViewModelScope</code>，请使用 <code>androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01</code> 或更高版本。</li>
<li>对于 <code>LifecycleScope</code>，请使用 <code>androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01</code> 或更高版本。</li>
<li>对于 <code>liveData</code>，请使用 <code>androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01</code> 或更高版本。 </li>
</ul>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs kotlin"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyViewModel</span>: <span class="hljs-type">ViewModel</span></span>() &#123;<br>    <span class="hljs-keyword">init</span> &#123;<br>        viewModelScope.launch &#123;<br>            <span class="hljs-comment">// Coroutine that will be canceled when the ViewModel is cleared.</span><br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs kotlin"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyFragment</span> : <span class="hljs-type">Fragment</span></span>() &#123;<br>    <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onViewCreated</span><span class="hljs-params">(view: <span class="hljs-type">View</span>, savedInstanceState: <span class="hljs-type">Bundle</span>?)</span></span> &#123;<br>        <span class="hljs-keyword">super</span>.onViewCreated(view, savedInstanceState)<br>        viewLifecycleOwner.lifecycleScope.launch &#123;<br>            <span class="hljs-keyword">val</span> params = TextViewCompat.getTextMetricsParams(textView)<br>            <span class="hljs-keyword">val</span> precomputedText = withContext(Dispatchers.Default) &#123;<br>                PrecomputedTextCompat.create(longTextContent, params)<br>            &#125;<br>            TextViewCompat.setPrecomputedText(textView, precomputedText)<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs kotlin"><span class="hljs-keyword">val</span> user: LiveData&lt;User&gt; = liveData &#123;<br>    <span class="hljs-keyword">val</span> <span class="hljs-keyword">data</span> = database.loadUser() <span class="hljs-comment">// loadUser is a suspend function.</span><br>    emit(<span class="hljs-keyword">data</span>)<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="依赖关系"><a href="#依赖关系" class="headerlink" title="依赖关系"></a>依赖关系</h4><ul>
<li><p>带有<code>-ktx</code>的库拥有 <code>kotlin</code> 的特性，有很多常用的扩展函数</p>
</li>
<li><p>带有<code>-ktx</code>的库依赖 java 版本， 带有<code>-ktx</code>和 java 版本各自单独使用即可</p>
</li>
<li><p><code>appcompat</code> 库包含 <code>fragmnet</code>,<code>fragment</code> 包含 <code>activity</code> ，当你引入<code>androidx</code> <code>appcompat</code> 库便可以使用<code>androidx</code> <code>fragment</code> 和 <code>androidx</code> <code>activity</code></p>
</li>
<li><p>受限于<code>appcompat</code> 稳定版的更新速度，您可以选择单独使用<code>androidx</code> <code>fragment</code>/<code>activity</code></p>
</li>
<li><p><code>AppCompatActivity</code> 继承自 <code>FragmentActivity</code>， <code>AppCompatActivity</code>可以直接使用 <code>fragment</code></p>
</li>
<li><p><code>DataBinding</code> <code>ViewBinding</code> 依赖于 <code>android build gradle</code> 插件 ,无需引入其他依赖</p>
</li>
<li><p><code>androidx</code> <code>fragment</code>/<code>activity</code>下均依赖的<code>ViewModel</code>和 <code>LiveData</code>，您可以直接使用</p>
</li>
<li><p>使用<code>ViewModel</code>和 <code>LiveData</code>完整功能需要单独引入，它们都是<code>lifecycle</code>大家族下的</p>
</li>
<li><p><code>lifecycle-extensions</code> 已废弃，不要使用它了</p>
</li>
<li><p>如果想使用实现<code>LifecycleOwner</code> 的 <code>Service</code> ，需要引入 <code>lifecycle-service</code></p>
</li>
</ul>
<h2 id="关于我"><a href="#关于我" class="headerlink" title="关于我"></a>关于我</h2><p>我是 <a href="https://flywith24.gitee.io/">Flywith24</a>，我的博客内容已经分类整理 <a href="https://github.com/Flywith24/BlogList" target="_blank" rel="noopener">在这里</a>，点击右上角的 Watch 可以及时获取我的文章更新哦 😉</p>
<ul>
<li><p><a href="https://juejin.im/user/57c7f6870a2b58006b1cfd6c" target="_blank" rel="noopener">掘金</a></p>
</li>
<li><p><a href="https://xiaozhuanlan.com/detail" target="_blank" rel="noopener">小专栏</a></p>
</li>
<li><p><a href="https://github.com/Flywith24" target="_blank" rel="noopener">Github</a></p>
</li>
</ul>
<p><img class="lazyload" data-original="https://user-gold-cdn.xitu.io/2020/6/26/172ee567fb4fbf7e?w=1954&h=624&f=jpeg&s=115362" src="/2020/03/01/Jetpack-dependencies/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt></p>
      </section>
      <section class="extra">
        
        <ul class="copyright">
  
  <li><strong>本文作者：</strong>杨云召（Flywith24）</li>
  <li><strong>本文链接：</strong><a href="https://flywith24.gitee.io/2020/03/01/Jetpack-dependencies/index.html">https://flywith24.gitee.io/2020/03/01/Jetpack-dependencies/index.html</a></li>
  <li><strong>版权声明：</strong>本博客所有文章均采用<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh"
      rel="external nofollow" target="_blank"> BY-NC-SA </a>许可协议，转载请注明出处！</li>
  
</ul>
        
        
        <section class="donate">
  <div class="qrcode">
    <img   class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20201015113301.jpg" src="" >
  </div>
  <div class="icon">
    <a href="javascript:;" id="alipay"><i class="iconfont iconalipay"></i></a>
    <a href="javascript:;" id="wechat"><i class="iconfont iconwechat-fill"></i></a>
  </div>
</section>
        
        
  <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Jetpack/">Jetpack</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MVVM/">MVVM</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/androidx/">androidx</a></li></ul>

        
<nav class="nav">
  
    <a href="/2020/03/02/Jetpack-fragment/"><i class="iconfont iconleft"></i>你真的会用Fragment吗？Fragment常见问题以及androidx下Fragment的使用新姿势</a>
  
  
    <a href="/2020/02/29/Jetpack-androidx-activity-fragment/">AdroidX 下使用 Activity 和 Fragment 的变化<i class="iconfont iconright"></i></a>
  
</nav>

      </section>
      
      <section class="comments">
  
  <div class="btn" id="comments-btn">查看评论</div>
  
  
<div id="valine"></div>
<script defer src="//unpkg.com/valine/dist/Valine.min.js"></script>
<script>
  window.onload = function () {
    var loadValine = function () {
      new Valine({
        el: '#valine',
        app_id: "SHA3gzCcxFlqaPcGRnX4UgET-gzGzoHsz",
        app_key: "OYo7ITS4AzTvBUvb1yIY8oaf",
        placeholder: "期待你的留言",
        avatar: "mp",
        pageSize: "10",
        lang: "zh-CN",
      });
    }
    if ( true ) {
      $("#comments-btn").on("click", function () {
        $(this).hide();
        loadValine();
      });
    } else {
      loadValine();
    }
  };
</script>

</section>
      
    </section>
  </div>
</article>
  </div>
</main>
  <footer class="footer">
  <div class="footer-social">
    
    
    
    
    
    <a href="tencent://message/?Menu=yes&uin=1032367864 " target="_blank" onMouseOver="this.style.color= '#12B7F5'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconQQ "></i>
    </a>
    
    
    
    
    
    <a href="javascript:; " target="_blank" onMouseOver="this.style.color= '#8bc34a'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconwechat-fill "></i>
    </a>
    
    
    
    
    
    <a href="https://flywith24.gitee.io/about/ " target="_blank" onMouseOver="this.style.color= '#d32f2f'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconheart "></i>
    </a>
    
    
    
    
    
    <a href="https://github.com/Flywith24 " target="_blank" onMouseOver="this.style.color= '#24292E'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  icongithub-fill "></i>
    </a>
    
    
    
    
    
    <a href="mailto:youngyunzhao@163.com " target="_blank" onMouseOver="this.style.color='#FFBE5B'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconmail"></i>
    </a>
    
  </div>
  <div class="footer-copyright"><p>Copyright© 2017 - 2020 ❤️ Flywith24</p></div>
</footer>
  
      <div class="fab fab-plus">
    <i class="iconfont iconplus"></i>
  </div>
  
  <div class="fab fab-daovoice">
    <i class="iconfont iconcomment"></i>
  </div>
  
  <div class="fab fab-up">
    <i class="iconfont iconcaret-up"></i>
  </div>
  
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":150,"height":300},"mobile":{"show":"tfalse"},"log":false});</script></body>

<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>




<script src="https://cdn.bootcdn.net/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js"></script>




<script src="https://cdn.bootcss.com/fancybox/3.5.7/jquery.fancybox.min.js"></script>




<script src="/js/utils.js"></script>
<script src="/js/modules.js"></script>
<script src="/js/zui.js"></script>
<script src="/js/script.js"></script>




<script>
  (function (i, s, o, g, r, a, m) {
    i["DaoVoiceObject"] = r;
    i[r] = i[r] || function () {
      (i[r].q = i[r].q || []).push(arguments)
    }, i[r].l = 1 * new Date();
    a = s.createElement(o), m = s.getElementsByTagName(o)[0];
    a.async = 1;
    a.src = g;
    a.charset = "utf-8";
    m.parentNode.insertBefore(a, m)
  })(window, document, "script", ('https:' == document.location.protocol ? 'https:' : 'http:') +
    "//widget.daovoice.io/widget/0f81ff2f.js", "daovoice")
  daovoice('init', {
    app_id: "7785620b"
  }, {
    launcher: {
      disableLauncherIcon: true,
    },
  });
  daovoice('update');
</script>



<script>
  (function () {
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
      bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    } else {
      bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
  })();
</script>


<script>
  var _hmt = _hmt || [];
  (function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?4c204d8bc027a0455b5fc642ac334ca8";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>










</html>